package ldk.test.looperserver;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class LooperServer {

    public static void main(String[] args) throws InterruptedException, IOException {
        ServerSocket serverSocket = new ServerSocket(9318);
        System.out.println("start");
        Socket socket = serverSocket.accept();
        System.out.println("received one socket");
        InputStream inputStream = socket.getInputStream();
        OutputStream outputStream = socket.getOutputStream();
        byte[] buf = new byte[1024];
        int read;
        try{
            while ((read = inputStream.read(buf)) >0){
                Thread.sleep(500);
                outputStream.write(buf, 0, read);
                try{
                    System.out.println("received: " +  new String(buf, 0, read));
                }catch (Exception e){/*ignore*/}
            }
        }catch (IOException e){
            System.out.println("error io");
        }finally {
            inputStream.close();
            outputStream.close();
            socket.close();
            serverSocket.close();
        }
        System.out.println("end");
    }
}
